package zucc.lcj.android_finalwork;


import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import zucc.lcj.android_finalwork.guide.GuideActivity;
import zucc.lcj.android_finalwork.help.HelpActivity;
import zucc.lcj.android_finalwork.me.MeActivity;
import zucc.lcj.android_finalwork.stars.StarsActivity;

public class FriendsActivity extends AppCompatActivity {
    private MyDatabaseHelper dbHelper;
    Button menput,womenput,menget,womenget;
    public static int global_user_id,global_user_age;
    public static String global_user_sex;
    public static int sexs;
    public static String mywx;

    @SuppressLint("Range")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friends);
        dbHelper = new MyDatabaseHelper(this,"UserStore.db",null,1);

        menput = (Button) findViewById(R.id.menput);
        womenput = (Button) findViewById(R.id.womenput);
        menget = (Button) findViewById(R.id.menget);
        womenget = (Button) findViewById(R.id.womenget);

//        男
        menput.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                String name = LoginActivity.global_login_user;
                Cursor cursor = db.rawQuery("select * from UserInfo where user_name=?",new String[]{name});
                if(cursor.moveToFirst()) {
//                    if(cursor.moveToFirst()==false)
//                        Toast.makeText(getApplicationContext(), "表中没有数据", Toast.LENGTH_SHORT).show();
                    int isput = cursor.getInt(cursor.getColumnIndex("isWechat"));
                    String sex = cursor.getString(cursor.getColumnIndex("user_sex"));
                    global_user_id = cursor.getInt(cursor.getColumnIndex("user_id"));
                    global_user_age = cursor.getInt(cursor.getColumnIndex("user_age"));
                    global_user_sex = cursor.getString(cursor.getColumnIndex("user_sex"));
                    if (isput == 1) {
                        Toast.makeText(getApplicationContext(), "您已放入", Toast.LENGTH_SHORT).show();
                    } else if (sex.equals("女")) {
                        Toast.makeText(getApplicationContext(), "这是放男同志微信的信箱", Toast.LENGTH_SHORT).show();
                    } else {
                        final EditText inputServer = new EditText(FriendsActivity.this);
                        AlertDialog.Builder builder = new AlertDialog.Builder(FriendsActivity.this);
                        builder.setTitle("输入您的微信号").setIcon(android.R.drawable.ic_dialog_email).setView(inputServer)
                                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                });
                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                String text = inputServer.getText().toString();
                                db.execSQL("update UserInfo set isWechat=1 where UserInfo.user_name=?",new String[]{name});

                                ContentValues values = new ContentValues();
                                values.put("wechatMen_user", global_user_id);
                                values.put("wechatMen_name", text);
                                values.put("wechatMen_sex", global_user_sex);
                                values.put("wechatMen_age", global_user_age);
                                db.insert("WechatMen", null, values);
                                values.clear();
                                Toast.makeText(getApplicationContext(), "放入成功", Toast.LENGTH_SHORT).show();
                            }
                        });
                        builder.show();
                    }
                }
            }
        });
        menget.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();

                if (LoginActivity.getGlobal_login_sex.equals("男")){
                    String sql="select * from WechatMen where wechatMen_user="+LoginActivity.global_login_user_id+"";
                    Cursor cursor2 = db.rawQuery(sql,null);
                    if (cursor2.moveToFirst()){
                        mywx=cursor2.getString(cursor2.getColumnIndex("wechatMen_name"));
                    }
                }
                else if (LoginActivity.getGlobal_login_sex.equals("女")){
                    String sql="select * from WechatWomen where wechatWomen_user='"+LoginActivity.global_login_user_id+"'";
                    Cursor cursor2 = db.rawQuery(sql,null);
                    if (cursor2.moveToFirst()){
                        mywx=cursor2.getString(cursor2.getColumnIndex("wechatWomen_name"));
                    }
                }
                System.out.println("frendsmywx"+mywx);
                Intent intent = new Intent(FriendsActivity.this,PayActivity.class);
                startActivity(intent);
                sexs=1;
            }
        });
//女
        womenput.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                String name = LoginActivity.global_login_user;
                Cursor cursor = db.rawQuery("select * from UserInfo where user_name=?",new String[]{name});
                if(cursor.moveToFirst()) {
//                    if(cursor.moveToFirst()==false)
//                        Toast.makeText(getApplicationContext(), "表中没有数据", Toast.LENGTH_SHORT).show();
                    int isput = cursor.getInt(cursor.getColumnIndex("isWechat"));
                    String sex = cursor.getString(cursor.getColumnIndex("user_sex"));
                    global_user_id = cursor.getInt(cursor.getColumnIndex("user_id"));
                    global_user_age = cursor.getInt(cursor.getColumnIndex("user_age"));
                    global_user_sex = cursor.getString(cursor.getColumnIndex("user_sex"));
                    if (isput == 1) {
                        Toast.makeText(getApplicationContext(), "您已放入", Toast.LENGTH_SHORT).show();
                    } else if (sex.equals("男")) {
                        Toast.makeText(getApplicationContext(), "这是放女同志微信的信箱", Toast.LENGTH_SHORT).show();
                    } else {

                        final EditText inputServer = new EditText(FriendsActivity.this);
                        AlertDialog.Builder builder = new AlertDialog.Builder(FriendsActivity.this);
                        builder.setTitle("输入您的微信号").setIcon(android.R.drawable.ic_dialog_email).setView(inputServer)
                                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        dialog.dismiss();
                                    }
                                });
                        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                String text = inputServer.getText().toString();
                                db.execSQL("update UserInfo set isWechat=1 where UserInfo.user_name=?",new String[]{name});

                                ContentValues values = new ContentValues();
                                values.put("wechatWomen_user", global_user_id);
                                values.put("wechatWomen_name", text);
                                values.put("wechatWomen_sex", global_user_sex);
                                values.put("wechatWomen_age", global_user_age);
                                db.insert("WechatWomen", null, values);
                                values.clear();
                                Toast.makeText(getApplicationContext(), "放入成功", Toast.LENGTH_SHORT).show();
                            }
                        });
                        builder.show();
                    }
                }
            }
        });
        womenget.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(FriendsActivity.this,PayActivity.class);
                startActivity(intent);
                sexs=2;

            }
        });




        ImageView bar_1 = (ImageView) findViewById(R.id.bar_1);
        ImageView bar_2 = (ImageView) findViewById(R.id.bar_2);
        ImageView bar_3 = (ImageView) findViewById(R.id.bar_3);
        ImageView bar_4 = (ImageView) findViewById(R.id.bar_4);
        ImageView bar_5 = (ImageView) findViewById(R.id.bar_5);
        bar_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(FriendsActivity.this, HelpActivity.class);
                startActivity(intent);
            }
        });
        bar_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(FriendsActivity.this, GuideActivity.class);
                startActivity(intent);
            }
        });
        bar_4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(FriendsActivity.this, StarsActivity.class);
                startActivity(intent);
            }
        });
        bar_5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(FriendsActivity.this, MeActivity.class);
                startActivity(intent);
            }
        });
    }

}
